package 栈和队列;

import java.util.EmptyStackException;

public class ArrayQueue {
    private int front = -1;
    private int rear = -1;
    private int array[];
    private int capacity = 1;


    public ArrayQueue() {
        array = new int[capacity];
    }

    public static ArrayQueue initQueue(int[] arr) {
        ArrayQueue queue = new ArrayQueue();
        for (int i : arr) {
            queue.enQueue(i);
        }
        return queue;
    }

    public void enQueue(int data) {
        if (isFullQueue()) {
            doubleQueue();
        }
        array[++rear] = data;
        if (front == -1) {
            front++;
        }
    }

    public boolean isEmpty() {
        return front == -1;
    }

    private void doubleQueue() {
        int newArray[] = new int[capacity * 2];
        System.arraycopy(array, 0, newArray, 0, array.length);
        capacity = capacity * 2;
        array = newArray;
    }

    public int deQueue() {
        if (isEmpty()) {
            throw new EmptyStackException();
        } else {
            int data = array[front++];
            return data;
        }
    }

    public boolean isFullQueue() {
        return rear == capacity - 1;

    }

    static class Main {
        public static void main(String[] args) {
            int[] a = {1, 3, 4, 5, 9};
            ArrayQueue stack = ArrayQueue.initQueue(a);
            System.out.println(stack.deQueue());
            System.out.println(stack.deQueue());
        }
    }
}
